package com.token.sysorg.services;
import java.sql.ResultSet;
import com.token.sql.IntegrateSql;
import com.token.sql.PrepareSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.base.FuncUtil;
import com.token.base.Pl;
/**
 * 读组织架构_返回tree的json结构
 * @author anger
 * @version 1.0
 */
public class FSysOrgRead_ForSelect{
	private static JSONArray arr = new JSONArray();
	public JSONArray ReadOrg_ForTree(FuncUtil F,IntegrateSql iSql) throws Exception {
		String orgtype = F.getUrlQueryString("orgtype");
		if(orgtype == null || orgtype.equals("1")){
			ReadUnit(F, iSql);
		}else if(orgtype.equals("2")){
			ReadDepartment(F, iSql, F.getUrlQueryString("comid", true, "公司"));
		}else if(orgtype.equals("3")){
			ReadPost(F, iSql);
		}else if(orgtype.equals("4")){
			ReadBenchmark(F, iSql);
		}
		return arr;
	}

	private static void ReadUnit(FuncUtil F,IntegrateSql iSql) throws Exception {
		String unitsql = "select ComID,UnitID,UnitName from F_Sys_Unit where SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("UnitID"));
			org.put("title", rs.getString("UnitName"));
			arr.add(org);
		}
	}

	private static void ReadDepartment(FuncUtil F,IntegrateSql iSql, String comid) throws Exception {
		String unitsql = "select DeptID,DepartmentNumber,DepartmentName,ComID from F_Sys_Department where ComID=? and SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, comid);
		psql.setPstValue(2, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("DepartmentNumber"));
			org.put("title", rs.getString("DepartmentName"));
			arr.add(org);
		}
	}

	private static void ReadPost(FuncUtil F,IntegrateSql iSql) throws Exception {
		String unitsql = "select PostNumber,PostName from F_Sys_Post where SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("PostNumber"));
			org.put("title", rs.getString("PostName"));
			arr.add(org);
		}
	}
	private static void ReadBenchmark(FuncUtil F,IntegrateSql iSql) throws Exception {
		String unitsql = "select BenchmarkID,BenchmarkName from f_sys_BenchmarkPost where SysID=?";
		PrepareSql psql = new PrepareSql(iSql);
		psql.setPrepare(unitsql);
		psql.setPstValue(1, Pl.getSysId());
		ResultSet rs = psql.executeQuery();
		while (rs.next()){
			JSONObject org = new JSONObject();
			org.put("id", rs.getString("BenchmarkID"));
			org.put("title", rs.getString("BenchmarkName"));
			arr.add(org);
		}
	}
}
